package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Permission;
import com.itheima.domain.system.User;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.mapper.system.PermissionDao;
import com.itheima.mapper.system.UserMapper;
import com.itheima.service.system.UserService;
import com.itheima.utils.MD5Util;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {
    @Override
    public User login(String username, String password) {
        UserMapper mapper = DaoInstanceFactory.getMapper(UserMapper.class);
        User user = mapper.findUnameAndPwd(username, password);
        return user;
    }

    @Override
    public void updatePassword(String password, String id) {
        UserMapper mapper = DaoInstanceFactory.getMapper(UserMapper.class);
        mapper.updatePassword(password, id);
    }

    @Override
    public boolean findEmail(String email) {
        UserMapper mapper = DaoInstanceFactory.getMapper(UserMapper.class);
        User user = mapper.findEmail(email);
        if (user != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int setPassword(String password, String email) {
        System.out.println("emailser = " + email);
        System.out.println("passwordser=" + password);
        UserMapper mapper = DaoInstanceFactory.getMapper(UserMapper.class);
        return mapper.setPassword(password, email);

    }

    @Override
    public List<Permission> findPermissionById(String id) {
        PermissionDao mapper = DaoInstanceFactory.getMapper(PermissionDao.class);
        return mapper.findPermissionByUserId(id);
    }
    @Override
    public void save(User user) {
        // 获取dao
        UserMapper userMapper = DaoInstanceFactory.getMapper(UserMapper.class);
        // id使用UUID的生成策略来获取
        String id = UUID.randomUUID().toString();
        user.setId(id);
        //密码必须经过加密处理MD5加密
        user.setPassword(MD5Util.md5(user.getPassword()));

        userMapper.save(user);
    }

    @Override
    public void delete(User user) {
        // 获取dao
        UserMapper userMapper = DaoInstanceFactory.getMapper(UserMapper.class);
        userMapper.delete(user);
    }

    @Override
    public void update(User user) {
        // 获取dao
        UserMapper userMapper = DaoInstanceFactory.getMapper(UserMapper.class);
        userMapper.update(user);
    }

    @Override
    public User findById(String id) {
        // 获取dao
        UserMapper userMapper = DaoInstanceFactory.getMapper(UserMapper.class);
        return userMapper.findById(id);
    }

    @Override
    public List<User> findAll() {
        // 获取dao
        UserMapper userMapper = DaoInstanceFactory.getMapper(UserMapper.class);
        return userMapper.findAll();
    }

    @Override
    public PageInfo findAll(int page, int size) {
        // 获取dao
        UserMapper userMapper = DaoInstanceFactory.getMapper(UserMapper.class);
        PageHelper.startPage(page,size);
        List<User> all = userMapper.findAll();
        return new PageInfo(all);
    }

    @Override
    public User  selectOne(String id) {
        // 获取dao
        UserMapper userMapper = DaoInstanceFactory.getMapper(UserMapper.class);
        return userMapper.selectOne(id);
    }



    @Override
    public void deleteByIds(String[] ids) {
        UserMapper userMapper = DaoInstanceFactory.getMapper(UserMapper.class);
        /*for (String id : ids) {
            userMapper.deleteById(id);
        }*/
        userMapper.deleteById(ids);
    }

    @Override
    public PageInfo findByName(int page, int size, String username) {
        // 获取dao
        UserMapper userMapper = DaoInstanceFactory.getMapper(UserMapper.class);
        PageHelper.startPage(page, size);
        List<User> list = userMapper.findByName(username);
        return new PageInfo(list);
    }

    @Override
    public void turnStatus(String id, String status) {
        UserMapper userMapper = DaoInstanceFactory.getMapper(UserMapper.class);
        int sta = Integer.parseInt(status);
        if (sta==0){
            sta=1;
            userMapper.turnStatus(id,sta);
        }else {
            sta=0;
            userMapper.turnStatus(id,sta);
        }
    }

}
